#!/usr/bin/env python

import config
import datastore
import json

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

class QuerySpotsInCategoryPage(webapp.RequestHandler):
    def get(self):
        # Get category key
        category_key = self.request.get('category_key').strip()
        if category_key == '':
            self.error(500)
            self.response.out.write(json.dumps({'msg': 'empty category key'}))
            return

        # Get category and spots
        category = datastore.HotSpotCategory.get(category_key)
        if category == None:
            self.error(500)
            self.response.out.write(json.dumps({'msg': 'category not found'}))
            return

        # Get spots
        rows = category.spot_set.fetch(config.MAX_SPOT_QUERY_NUM)
        spots = map(lambda x: { 'key': str(x.key()),
                                'name': x.name,
                                'lat': x.location.lat,
                                'lon': x.location.lon }, rows)

        self.response.out.write(json.dumps(spots))
